package com.handle.oauth.config;

import com.handle.common.core.constants.Constant;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;

/**
 * @author zhangqing
 * @version V1.0
 * @description redisson 配置文件装配类
 * @date 4/12/2020 7:50 PM
 */
@Configuration
public class RedissonConfig {
    private static final Logger log = LoggerFactory.getLogger(RedissonConfig.class);

    private final Environment env;

    public RedissonConfig(Environment env) {
        this.env = env;
    }
    @Bean
    public RedissonClient redissonClient() throws IOException {
        log.info("初始化Auth服务,Redisson配置");
        Collection<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
        Config config= null;
        if(activeProfiles.contains(Constant.DEV) || activeProfiles.contains(Constant.TEST)){
            config = Config.fromYAML(new ClassPathResource("redisson-single.yaml").getInputStream());
        }
        if(activeProfiles.contains(Constant.PRO)){
            config = Config.fromYAML(new ClassPathResource("redisson-cluster.yaml").getInputStream());
        }
        assert config != null;
        return Redisson.create(config);
    }

}
